package graduate.cslg.com.universityshopping.mainPage;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.PunchRequest;
import graduate.cslg.com.universityshopping.net.PunchResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PunchActivity extends AppCompatActivity {

    private TextView morningPunch;
    private TextView sportClub;
    private TextView chinUp;
    private TextView basketCom;
    private TextView athletics;
    private TextView sportItem;
    private TextView competitionTime;
    private TextView checkWork4;
    private TextView checkWork5;
    private TextView addTime;
    private TextView userName;
    private String getAccount;
    private PunchResponse mPunchResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_punch);
        initViews();
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getAccount = preferences.getString("user_account", "Z09315136");
        userName.setText(preferences.getString("user_name", "Tom Wang") + "同学");
        getPunchInfo();
    }

    private void initViews() {
        morningPunch = findViewById(R.id.morning_punch);
        sportClub = findViewById(R.id.sport_club_time);
        chinUp = findViewById(R.id.chin_up_time);
        basketCom = findViewById(R.id.basketball_time);
        athletics = findViewById(R.id.athletics_time);
        sportItem = findViewById(R.id.sport_item_time);
        competitionTime = findViewById(R.id.competition_time);
        checkWork4 = findViewById(R.id.check_work_4);
        checkWork5 = findViewById(R.id.check_work_5);
        addTime = findViewById(R.id.add_time);
        userName = findViewById(R.id.user_name);
    }

    private void getPunchInfo() {
        PunchRequest punchRequest = new PunchRequest();
        punchRequest.setUsername(getAccount);
        punchRequest.setPassword(getAccount);
        RetrofitDevelop.getPunchService()
                .getPunchInfo(punchRequest)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PunchResponse>() {
                    @Override
                    public void onCompleted() {
                        morningPunch.setText(mPunchResponse.getMorning_punch());
                        sportClub.setText(mPunchResponse.getSport_club());
                        chinUp.setText(mPunchResponse.getChin_up());
                        basketCom.setText(mPunchResponse.getBasket_com());
                        athletics.setText(mPunchResponse.getAthletics());
                        sportItem.setText(mPunchResponse.getSport_item());
                        competitionTime.setText(mPunchResponse.getCompetition_time());
                        checkWork4.setText(mPunchResponse.getCheckWork4());
                        checkWork5.setText(mPunchResponse.getCheckWork5());
                        addTime.setText(mPunchResponse.getAddTime());
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(PunchActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(PunchResponse punchResponse) {
                        mPunchResponse = punchResponse;
                    }
                });
    }
}
